package cn.com.self.apis.collect.service.impl;

import cn.com.self.apis.collect.entity.CollectionInfo;
import cn.com.self.apis.collect.entity.CollectionInfoVO;
import cn.com.self.apis.collect.mapper.CollectionInfoMapper;
import cn.com.self.apis.collect.service.ICollectionInfoService;
import cn.com.self.apis.member.entity.UMemberInfoRelation;
import cn.com.self.apis.member.service.IMemberInfoRelationService;
import cn.com.self.apis.portal.util.CustomPage;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangwt
 * @since 2020-05-29
 */
@Service
public class CollectionInfoServiceImpl extends BaseServiceImpl<CollectionInfo, String> implements ICollectionInfoService {

    @Autowired
    private CollectionInfoMapper collectionInfoMapper;
    @Autowired
    private IMemberInfoRelationService memberRelationService;

    @Override
    public ResponseEntity loadCollectionInfoList(Map<String, Object> queryParams, Pageable pageable) {
        if (queryParams.get("memberId") == null) {
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(403, "会员id不能为空！"));

        }
        CustomPage<CollectionInfoVO> page = new CustomPage<>(pageable.getPageNumber(), pageable.getPageSize());
        if ("1".equals(queryParams.get("collectionType"))) {
            //收藏需求
            ICustomPage<CollectionInfoVO> collectionList = collectionInfoMapper.getCollRequementList(page, queryParams);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(collectionList)));
        } else if ("2".equals(queryParams.get("collectionType"))) {
            //收藏用户
            ICustomPage<CollectionInfoVO> collectionList = collectionInfoMapper.getCollMemberList(page, queryParams);
            for (CollectionInfoVO collection : collectionList.getRows()) {
                List<UMemberInfoRelation> list = memberRelationService.queryByMemberInfoId(collection.getCollectionId().toString());
                collection.setMemberInfoRelationList(list);
            }
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(collectionList)));
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem()));
    }
}
